﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Drawing;
using System.Data;
using Gollie.DataTables;

namespace Gollie.Controls
{
	public partial class MyHistory : AuthenticationRequiredCustomerBaseUserControl
	{
		internal static Color colorGreen = Color.FromArgb(179, 213, 181);
		internal static Color colorRed = Color.FromArgb(253, 81, 77);
		internal DateTime FilterDateFrom;
		internal DateTime FilterDateTo;
		internal uint FilterVendorId;

		protected override void OnPostBack(object sender, EventArgs e)
		{
			this.btnSearch_Click(null, null);

			//this.CustomizeView();
		}

		protected override void OnFirstHit(object sender, EventArgs e)
		{
			this.tbFilterDateTo.Text = ObjectFormatter.GetShortDateString(DateTime.Today);
			this.tbFilterDateFrom.Text = ObjectFormatter.GetShortDateString(DateTime.Today.AddMonths(-1));

			MyTransactions myTransactions = MyTransactions.GetInstance(this.UserInfo.Id);

			this.ddlVendors.DataTextField = "Key";
			this.ddlVendors.DataValueField = "Value";
			this.ddlVendors.DataSource = myTransactions.vendors;
			this.ddlVendors.DataBind();
			this.ddlVendors.SelectedValue = "0";

			this.SetFilterArgs();
			this.GridView1.DataSource = myTransactions.Filter(this.FilterFunction);

			this.GridView1.DataBind();

			this.CustomizeView();
		}

		private void CustomizeView()
		{
			foreach (GridViewRow row in this.GridView1.Rows)
			{
				VendorPresenter vendorPresenter = LoadControl("VendorPresenter.ascx") as VendorPresenter;
				HiddenField v = row.Cells[0].FindControl("fldVendorId") as HiddenField;
				HiddenField b = row.Cells[0].FindControl("fldBedrijfsnaam") as HiddenField;
				HiddenField w = row.Cells[0].FindControl("fldWebsite") as HiddenField;
				HiddenField p = row.Cells[0].FindControl("fldPlaats") as HiddenField;
				HiddenField s = row.Cells[0].FindControl("fldStraat") as HiddenField;
				vendorPresenter.VendorId = Convert.ToInt32(v.Value);
				vendorPresenter.Bedrijfsnaam = b.Value;
				vendorPresenter.Website = w.Value;
				vendorPresenter.City = p.Value;
				vendorPresenter.Street = s.Value;
				vendorPresenter.UseXXSmall = true;

				row.Cells[0].Controls.Add(vendorPresenter);
			}
		}

		internal bool FilterFunction(MyTransactionsItem item)
		{
			bool retVal = (this.FilterVendorId == 0 || item.VendorId == this.FilterVendorId)
							&& (this.FilterDateFrom == DateTime.MinValue || item.Tijdstip >= this.FilterDateFrom)
							&& (this.FilterDateTo == DateTime.MaxValue || item.Tijdstip <= this.FilterDateTo);

			return retVal;
		}

		private void SetFilterArgs()
		{
			this.FilterVendorId = Convert.ToUInt32(this.ddlVendors.SelectedValue);
			this.FilterDateFrom = DateTime.MinValue;
			this.FilterDateFrom = DateTime.MinValue;

			string filterDateFrom = this.tbFilterDateFrom.Text.Trim().ToLower();
			string filterDateTo = this.tbFilterDateTo.Text.Trim().ToLower();

			DateTime from;
			DateTime to;

			if (string.IsNullOrEmpty(filterDateFrom) == false)
			{
				if (DateTime.TryParseExact(filterDateFrom, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out from))
				{
					this.FilterDateFrom = from;
					this.tbFilterDateFrom.BackColor = System.Drawing.Color.White;
				}
				else
					this.tbFilterDateFrom.BackColor = System.Drawing.Color.Red;
			}

			if (string.IsNullOrEmpty(filterDateTo) == false)
			{
				if (DateTime.TryParseExact(filterDateTo, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out to))
				{
					this.FilterDateTo = to;
					this.tbFilterDateTo.BackColor = System.Drawing.Color.White;
				}
				else
					this.tbFilterDateTo.BackColor = System.Drawing.Color.Red;
			}
		}

		protected void btnSearch_Click(object sender, ImageClickEventArgs e)
		{
			MyTransactions myTransactions = MyTransactions.GetInstance(this.UserInfo.Id);

			this.SetFilterArgs();
			this.GridView1.DataSource = myTransactions.Filter(this.FilterFunction);
			this.GridView1.DataBind();

			this.CustomizeView();
		}

		protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				var cell = e.Row.Cells[4];
				if (cell.Text.Contains('-'))
					cell.BackColor = colorRed;
				else
					cell.BackColor = colorGreen;
			}
		}

		protected SortDirection SortDir
		{
			get
			{
				if (ViewState["dirState"] == null)
					ViewState["dirState"] = SortDirection.Ascending;

				return (SortDirection)ViewState["dirState"];
			}
			set
			{
				ViewState["dirState"] = value;
			}
		}


		protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
		{
			MyTransactions myTransactions = MyTransactions.GetInstance(this.UserInfo.Id);

			SortDir = SortDir == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;
			List<MyTransactionsItem> list = myTransactions.Sort(e.SortExpression, SortDir == SortDirection.Ascending);

			this.GridView1.DataSource = list.Where(item => this.FilterFunction(item)).ToList();
			this.GridView1.DataBind();
			this.CustomizeView();
		}
	}
}